package com.example.springbootapiintegrat.qq.server.Impl;

import cn.hutool.http.HttpUtil;
import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.elasticsearch.core.SearchResponse;
import co.elastic.clients.elasticsearch.core.search.Hit;
import co.elastic.clients.json.jackson.JacksonJsonpMapper;
import co.elastic.clients.transport.ElasticsearchTransport;
import co.elastic.clients.transport.rest_client.RestClientTransport;
import com.alipay.api.domain.Product;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.springbootapiintegrat.qq.bean.Book;
import com.example.springbootapiintegrat.qq.bean.Leven;
import com.example.springbootapiintegrat.qq.common.LevenConfig;
import com.example.springbootapiintegrat.qq.mapper.LevenMapper;
import com.example.springbootapiintegrat.qq.server.ILevenService;
import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author qrn
 * @Title https://blog.csdn.net/qq_4197108
 * @Date 2021/5/27 19:59
 * @time 19:59
 */
@Service
public class LevenServiceImpl extends ServiceImpl<LevenMapper, Leven> implements ILevenService {

    /**
     *根据qq号获取qq等级:
     * @param qq
     * @return
     */
    @Override
    public String queryQQ(String qq) {
//        Map<String,Object> map = new HashMap<>();
//        map.put("qq",qq);
//        String data = HttpUtil.get(LevenConfig.QQLEVEL, map);
//        return data;
        RestClient restClient = RestClient.builder(
                new HttpHost("localhost", 9200)).build();
        ElasticsearchTransport transport = new RestClientTransport(
                restClient, new JacksonJsonpMapper());

        ElasticsearchClient client = new ElasticsearchClient(transport);
        try{
            SearchResponse<Book> search = client.search(s -> s
                            .index("test")
                            .query(q -> q
                                    .term(t -> t
                                            .field("name")
                                            .value(v -> v.stringValue("小明"))
                                    )),
                    Book.class);

            System.out.println(search);
        }catch (IOException e){
            e.printStackTrace();
        }
        return  "";
    }

    /**
     * 直连qq群:
     * @param qq
     * @return
     */
    @Override
    public String queryQun(String qq) {
        Map<String,Object> map = new HashMap<>();
        map.put("qq",qq);
        String data = HttpUtil.get(LevenConfig.QQQUN, map);
        return data;
    }
}
